package com.lanou.tipsy.adapter.recommend;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lanou.tipsy.R;
import com.lanou.tipsy.entity.recommend.songlist_recommend.SongsRecommendListEntity;

import java.util.ArrayList;

/**
 * Created by wangshuai on 15/10/30.
 * 歌单推荐适配器
 */
public class SongsRecommendAdapter extends BaseAdapter {

    private Context mContext;

    private ArrayList<SongsRecommendListEntity> srl;

    public SongsRecommendAdapter(Context mContext, ArrayList<SongsRecommendListEntity> srl) {
        this.mContext = mContext;
        this.srl = srl;


    }


    @Override
    public int getCount() {
       return srl != null && srl.size() > 0 ? srl.size() : 0;
    }

    @Override
    public SongsRecommendListEntity getItem(int position) {

        return srl != null && srl.size() > 0 ? srl.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHandler handler = null;
        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.gridview_recommend_songs,null);
            handler = new ViewHandler();
            handler.songRecommendGvTv = (TextView) convertView.findViewById(R.id.songRecommendGvTv);
            handler.songRecommendGvIv = (SimpleDraweeView) convertView.findViewById(R.id.songRecommendGvIv);
            handler.listnumTv = (TextView) convertView.findViewById(R.id.listnumTv);
            convertView.setTag(handler);
        }else {
            handler = (ViewHandler) convertView.getTag();
        }
        SongsRecommendListEntity s = getItem(position);
        if (s != null){
            handler.songRecommendGvTv.setText(s.getTitle());
            Uri uri = Uri.parse(s.getPic());
            handler.songRecommendGvIv.setImageURI(uri);

            handler.listnumTv.setText(s.getListenum());
        }

        return convertView;
    }
    private class ViewHandler{
        public TextView songRecommendGvTv;
        public SimpleDraweeView songRecommendGvIv;
        public TextView listnumTv;
    }

}
